<?php
/**
* GetVillage get village matrix based on array of data
*
* Long description first sentence starts here
* and continues on this line for a while
* finally concluding here at the end of
* this paragraph
*
* @package    ABHO | SSCF | SGD
* @subpackage
* @author     Olav Bringedal <olav@jaggu.org>
* @version    $Id: getVillage.php, v 0.22 Thu Feb 25 2010 10:03:20 GMT+0100 Antonio Jozzolino $
* @since      Tue Feb 23 2010 17:58:14 GMT+0100
* @access     public
* @see        http://thefivepillars.org
* @uses       
* @example    relativepath/to/example.php  description
*/
class GetVillage
{
    var $village;
    var $counter;
    var $realcounter;
    
    /**
    --svn import village https://drawvillage.googlecode.com/svn/village -m "init" --username olav.bringedal@gmail.com
    
    * GetVillage get village matrix based on array of data
    *   We use php to make a dataset based on a xml matrix or a php array
    *    
    *   Landtypes:  Mountain, Swamp, Glacier, Plain, Forest
    *   
    *   Ultimately we'd use a pentagram to have these "grow" from the borders of each edge. But that will prolly be to difficult for now.
    *   We settle for a square grid that have plain in Glacier in the middle and the others from the corners.
    *    
    *   Buildings are: Houses, Workshops, Temples, Barracks, Forts, Barriers
    *   And lastly: Wilderness
    *    
    *   Algoritm:
    *   make a matrix, populate each cell with a land type based on what the mage got.
    *
    * @param   array [$landinfo] assoc array with keys Land, Houses, Workshops, Temples, Barracks, Forts, Barriers, Wilderness, Mountain, Swamp, Glacier, Plain, Forest
    * @return  array a matrix of 
    * @author  Olav Bringedal <olav@jaggu.org>
    * @since   Tue Feb 23 2010 12:21:31 GMT+0100
    * @version v 0.00 Tue Feb 23 2010 12:21:31 GMT+0100
    * @todo    lots
    */

    function GetVillage($landinfo)
    {
        // Calculate height width of matrix
       
        //OBR 23/2-10 sqare for now
        //1000->10
        //5000->50
        //10000->100
        $scale=round($landinfo['land']/1000)*10;
        $this->village['width']=$this->village['height']=round(sqrt($landinfo['land']/$scale))+1;
             
        //print_r ($landinfo);
       
       // Start populate the matrix
       //   Mountain upper left
       $startCoord['x']=0;
       $startCoord['y']=0;
       for($i=0; $i< $landinfo['mountains']; $i+=$scale)
       {
           
            $this->placeLand ($startCoord, 'M');
       }
      
       //   Swamp upper right
        $startCoord['x']=$this->village['width'];
        $startCoord['y']=0;
        for($i=0; $i< $landinfo['swamps']; $i+=$scale)
        {
           
            $this-> placeLand ($startCoord, 'S');
        }
       
       //   Forest lower left
        $startCoord['x']=0;
        $startCoord['y']=$this->village['height'];
        for($i=0; $i< $landinfo['forests']; $i+=$scale)
        {
           
            $this->placeLand ($startCoord, 'F');
        }
       
        //   plains lower right
        $startCoord['x']=$this->village['width'];
        $startCoord['y']=$this->village['height'];
        
        for($i=0; $i< $landinfo['plains']; $i+=$scale)
        {
            
            $this->placeLand ($startCoord, 'D');
        }
       
        //   glacier middle
        $startCoord['x']=round($this->village['width']/2);
        $startCoord['y']=round($this->village['height']/2);
        for($i=0; $i< $landinfo['glaciers']; $i+=$scale)
        {
           
            $this-> placeLand ($startCoord, 'G');
        }
        
        // Buildings Houses, Workshops, Temples, Barracks, Forts, Barriers
        
        //   houses middle
        $startCoord['x']=round($this->village['width']/3);
        $startCoord['y']=round($this->village['height']/3);
        for($i=0; $i< $landinfo['houses']; $i+=$scale)
        {
           
            $this-> placeLand ($startCoord, 'H');
        }
        
        //   workshops middle
        $startCoord['x']=$this->village['width']-round($this->village['width']/3);
        $startCoord['y']=$this->village['height']-round($this->village['height']/3);
        for($i=0; $i< $landinfo['workshops']; $i+=$scale)
        {
           
            $this-> placeLand ($startCoord, 'W');
        }
        
        //   temples middle
        $startCoord['x']=round($this->village['width']/2);
        $startCoord['y']=$this->village['height']-round($this->village['height']/3);
        for($i=0; $i< $landinfo['temples']; $i+=$scale)
        {
           
            $this-> placeLand ($startCoord, 'T');
        }
        
        //   barracks middle
        $startCoord['x']=$this->village['width']-round($this->village['width']/3);
        $startCoord['y']=round($this->village['height']/2);
        for($i=0; $i< $landinfo['barracks']; $i+=$scale)
        {
            $this-> placeLand ($startCoord, 'B');
        }
       $this->printTiles($scale);
    }
   
    /**
    *  placeLand put a land in village matrix based free or not and direction
    *    
    *  if free: place
    *  else: got either $move[0] or $move[1]
    *  if $move=0 go in any direction
    *   
    *
    * @param   array [$landinfo] assoc array with keys Land, Houses, Workshops, Temples, Barracks, Forts, Barriers, Wilderness, Mountain, Swamp, Glacier, Plain, Forest
    * @global  array $village matrix 
    * @author  Olav Bringedal <olav@jaggu.org>
    * @since   Tue Feb 23 2010 12:33:31 GMT+0100
    * @version v 0.00 Tue Feb 23 2010 12:33:31 GMT+0100
    * @todo    lots
    */
    
    
    function placeLand ($startCoord, $landtype, $move=0)
    {
        
        $this->counter++;
        $this->counter2++;
        $this->realcounter++;
        
        if ($this->counter>1000)
        {
          //try again
          print ("<br>trying again");
            $this->counter=0;
            $startCoord['x']=0;
            $startCoord['y']=0;
        }
        if ($this->counter2>10000)
        {
           print ("<br>giving up");
           $this->counter2=0;
            //give up
            return;
        }
        
        if (!$this->village[$startCoord['x']][$startCoord['y']]
        && $startCoord['x'] < $this->village['width']
        && $startCoord['x']>= 0
        && $startCoord['y'] < $this->village['height']
        && $startCoord['y'] >= 0
        )
        {
            $this->village[$startCoord['x']][$startCoord['y']]=$landtype;
        }
        else
        {
            //print ("<br>trying {$startCoord['x']} {$startCoord['y']} to $landtype");
            // move in a direction
            if (!$move)
            {
                $move=mt_rand(1,4);
                //print ("<br>move $move");
                
                if ($move==1 && $startCoord['x']<$this->village['width'])
                {
                    $startCoord['x']+=1;
                }
                elseif ($move==2 && $startCoord['x']>0)
                {
                    $startCoord['x']-=1;
                }
                elseif ($move==3 && $startCoord['y']<$this->village['height'])
                {
                   $startCoord['y']+=1;
                }
                elseif ($move==4 && $startCoord['y']>0)
                {
                   $startCoord['y']-=1;
                }
                // if we dont get a hit, i.e. outside of range, just try again...
            }
            $this->placeLand ($startCoord, $landtype, 0);
        }
        $this->counter=0;
        //$this->counter2=0;
    }
    
    function printTiles ($scale)
    {
        print ("<br>Each tile is about $scale land");
             
        
        for($y=0; $y< $this->village['height']; $y++)
        {
            print ("<div style=\"padding:0;margin:0\">");
            for($x=0; $x< $this->village['width']; $x++)
            {
                $edges=$this->getEdges($x,$y);
                print ("<span style=\"min-width:40px;height:40px;display:inline-block;margin:0px;padding:0;\" class=\"{$this->village[$x][$y]}\" title=\"{$this->village[$x][$y]}\"><img border=0 src=\"images/tiles/{$this->village[$x][$y]}.$edges.png\"></span>");
            }
            print ("</div>");
        }
        print ($this->realcounter);
    }
    function getEdges($x, $y)
    {
        $thisland=$this->village[$x][$y];
        
        if (strstr("MSGDF",$thisland))  //only for terrain
        {
            //up
            if ($this->village[$x][$y-1]==$thisland || $y==0)
            {
                $edges[]="1";
            } else
            {
                $edges[]="0";
            }
            //down
            if ($this->village[$x][$y+1]==$thisland ||$y==$this->village['height'])
            {
                $edges[]="1";
            } else
            {
                $edges[]="0";
            }
            //left
            if ($this->village[$x-1][$y]==$thisland || $x==0)
            {
                $edges[]="1";
            } else
            {
                $edges[]="0";
            }
            //right
            if ($this->village[$x+1][$y]==$thisland || $x==$this->village['width'])
            {
                $edges[]="1";
            } else
            {
                $edges[]="0";
            }
            
            return implode(".", $edges);
            }
    }
    
   
}
// test 
require_once("include/db.php");
require_once("include/globalvars.php");
require_once("include/modfunctions.php");
require_once("include/auth.inc.php");
require_once("include/take_turn.php");


$turn=unserialize('a:92:{s:12:"PlayerNumber";s:1:"2";s:12:"tournamentID";s:1:"0";s:9:"loginname";s:6:"olabri";s:10:"PlayerName";s:7:"Zebulon";s:11:"TimeStarted";s:10:"1260964200";s:12:"TimeModifier";s:5:"89480";s:10:"spelllevel";s:1:"5";s:4:"Land";s:3:"600";s:6:"houses";s:3:"200";s:8:"barracks";s:2:"30";s:7:"temples";s:2:"20";s:5:"forts";s:1:"3";s:10:"TurnsTaken";s:1:"0";s:10:"recruiting";s:1:"0";s:12:"recruitingno";s:1:"0";s:9:"mountains";s:2:"40";s:6:"swamps";s:2:"40";s:6:"status";N;s:9:"workshops";s:2:"40";s:6:"plains";s:2:"40";s:10:"population";s:5:"60000";s:4:"gold";s:7:"2000000";s:4:"mana";s:4:"5000";s:7:"forests";s:2:"40";s:8:"barriers";s:1:"0";s:8:"glaciers";s:2:"40";s:5:"wmana";s:4:"5000";s:5:"omana";s:4:"5000";s:5:"nmana";s:4:"5000";s:5:"emana";s:4:"5000";s:9:"alignment";s:8:"Mountain";s:12:"defence_item";s:1:"0";s:13:"defence_spell";s:1:"0";s:17:"defence_item_rate";s:1:"0";s:18:"defence_spell_rate";s:1:"0";s:9:"net_power";s:6:"729900";s:4:"rank";s:3:"221";s:11:"description";N;s:14:"override_align";s:8:"Mountain";s:6:"morale";s:1:"0";s:6:"taxing";s:1:"0";s:10:"meditation";s:1:"0";s:7:"advisor";s:2:"54";s:8:"language";s:2:"EN";s:5:"image";s:0:"";s:5:"kills";s:1:"0";s:5:"arrow";N;s:2:"xp";s:4:"2000";s:7:"visible";s:1:"1";s:4:"lock";s:1:"0";s:4:"mini";s:1:"0";s:9:"turn_info";s:1:"1";s:2:"sp";s:1:"0";s:14:"old_playername";s:0:"";s:5:"rrank";s:3:"221";s:5:"fb_id";s:9:"535967610";s:12:"playernumber";s:1:"2";s:4:"land";s:3:"600";s:10:"SpellLevel";s:1:"5";s:5:"pmana";s:4:"5000";s:5:"money";s:7:"2000000";s:3:"pop";s:5:"60000";s:11:"turns_taken";s:1:"0";s:2:"np";R:37;s:3:"onp";s:6:"729900";s:3:"oxp";s:4:"2000";s:8:"mage_lvl";i:2;s:9:"omage_lvl";i:2;s:6:"awards";N;s:8:"building";a:21:{s:9:"node_cost";N;s:11:"houses_cost";s:2:"70";s:17:"houses_cost_turns";s:3:"500";s:13:"barracks_cost";s:3:"250";s:19:"barracks_cost_turns";s:3:"750";s:14:"workshops_cost";s:3:"100";s:20:"workshops_cost_turns";s:4:"1500";s:12:"temples_cost";s:3:"100";s:18:"temples_cost_turns";s:4:"2000";s:10:"forts_cost";s:4:"3000";s:16:"forts_cost_turns";s:5:"30000";s:11:"plains_cost";s:3:"150";s:17:"plains_cost_turns";s:3:"200";s:13:"barriers_cost";s:2:"50";s:18:"barriers_cost_mana";s:2:"30";s:8:"up_money";i:36500;s:8:"up_wmana";i:0;s:8:"up_omana";i:0;s:8:"up_emana";i:0;s:8:"up_pmana";i:0;s:8:"up_nmana";i:0;}s:11:"barriercost";i:0;s:4:"opop";s:5:"60000";s:6:"omoney";s:7:"2000000";s:10:"wmana_pool";i:45000;s:10:"omana_pool";i:45000;s:10:"emana_pool";i:45000;s:10:"pmana_pool";i:45000;s:10:"nmana_pool";i:45000;s:6:"owmana";s:4:"5000";s:6:"oomana";s:4:"5000";s:6:"oemana";s:4:"5000";s:6:"opmana";s:4:"5000";s:6:"onmana";s:4:"5000";s:8:"wmana_in";d:317;s:8:"omana_in";d:317;s:8:"emana_in";d:317;s:8:"pmana_in";d:317;s:8:"nmana_in";d:317;s:10:"wilderness";i:107;s:4:"army";a:10:{s:6:"up_pop";i:0;s:8:"up_emana";i:0;s:8:"up_nmana";i:0;s:8:"up_wmana";i:0;s:8:"up_omana";i:0;s:8:"up_pmana";i:0;s:8:"up_money";d:175;i:6102;a:59:{s:12:"PlayerNumber";s:1:"2";s:6:"unitID";s:4:"6102";s:9:"noofunits";s:3:"100";s:14:"unitexperience";s:1:"0";s:8:"unitname";s:7:"Archers";s:13:"unitalignment";s:7:"Neutral";s:12:"unitstrenght";s:3:"110";s:13:"unitstrenght2";s:1:"0";s:17:"unitcounterattack";s:1:"0";s:9:"unitinit1";s:1:"2";s:9:"unitinit2";s:1:"0";s:6:"unithp";s:2:"60";s:6:"unitNP";s:1:"9";s:14:"unitattacktype";s:3:"R.M";s:15:"unitattacktype2";s:0:"";s:8:"unittype";s:1:"R";s:13:"unitcostmoney";s:3:"1.5";s:11:"unitcostpop";s:1:"0";s:13:"unitcostwmana";s:1:"0";s:13:"unitcostomana";s:1:"0";s:13:"unitcostemana";s:1:"0";s:12:"unitcostmana";s:1:"0";s:13:"unitcostnmana";s:1:"0";s:13:"unitabilities";s:0:"";s:4:"Mres";s:2:"20";s:4:"Rres";s:2:"10";s:4:"Ires";s:1:"0";s:4:"Pres";s:1:"0";s:5:"Mgres";s:1:"0";s:4:"Fres";s:1:"0";s:5:"Prres";s:1:"0";s:4:"Hres";s:1:"0";s:4:"Lres";s:1:"0";s:5:"Psres";s:1:"0";s:4:"Bres";s:1:"0";s:7:"C_M_Res";s:1:"0";s:7:"C_S_Res";s:1:"0";s:7:"C_E_Res";s:1:"0";s:7:"C_P_Res";s:1:"0";s:7:"C_N_Res";s:1:"0";s:11:"recruitable";s:3:"100";s:4:"Verb";s:10:" shoots at";s:4:"race";s:5:"Human";s:13:"cast_modifier";s:5:"-1000";s:12:"unitaccuracy";s:2:"36";s:5:"dodge";s:2:"40";s:7:"healing";s:1:"0";s:2:"id";s:4:"6102";s:4:"type";s:1:"U";s:4:"name";s:7:"Archers";s:4:"lang";s:2:"EN";s:8:"battleNP";s:6:"900.00";s:8:"up_money";d:150;s:8:"up_pmana";i:0;s:8:"up_omana";i:0;s:8:"up_wmana";i:0;s:8:"up_nmana";i:0;s:8:"up_emana";i:0;s:6:"up_pop";i:0;}s:8:"totunits";i:200;i:6101;a:59:{s:12:"PlayerNumber";s:1:"2";s:6:"unitID";s:4:"6101";s:9:"noofunits";s:3:"100";s:14:"unitexperience";s:1:"0";s:8:"unitname";s:8:"Peasants";s:13:"unitalignment";s:7:"Neutral";s:12:"unitstrenght";s:2:"70";s:13:"unitstrenght2";s:1:"0";s:17:"unitcounterattack";s:1:"0";s:9:"unitinit1";s:1:"1";s:9:"unitinit2";s:1:"0";s:6:"unithp";s:2:"60";s:6:"unitNP";s:1:"5";s:14:"unitattacktype";s:1:"M";s:15:"unitattacktype2";N;s:8:"unittype";s:1:"M";s:13:"unitcostmoney";s:4:"0.25";s:11:"unitcostpop";s:1:"0";s:13:"unitcostwmana";s:1:"0";s:13:"unitcostomana";s:1:"0";s:13:"unitcostemana";s:1:"0";s:12:"unitcostmana";s:1:"0";s:13:"unitcostnmana";s:1:"0";s:13:"unitabilities";N;s:4:"Mres";s:1:"0";s:4:"Rres";s:1:"0";s:4:"Ires";s:1:"0";s:4:"Pres";s:1:"0";s:5:"Mgres";s:1:"0";s:4:"Fres";s:1:"0";s:5:"Prres";s:1:"0";s:4:"Hres";s:1:"0";s:4:"Lres";s:1:"0";s:5:"Psres";s:1:"0";s:4:"Bres";s:1:"0";s:7:"C_M_Res";s:1:"0";s:7:"C_S_Res";s:1:"0";s:7:"C_E_Res";s:1:"0";s:7:"C_P_Res";s:1:"0";s:7:"C_N_Res";s:1:"0";s:11:"recruitable";s:3:"150";s:4:"Verb";N;s:4:"race";s:5:"Human";s:13:"cast_modifier";s:5:"-1000";s:12:"unitaccuracy";s:2:"36";s:5:"dodge";s:2:"40";s:7:"healing";s:1:"0";s:2:"id";s:4:"6101";s:4:"type";s:1:"U";s:4:"name";s:8:"Peasants";s:4:"lang";s:2:"EN";s:8:"battleNP";s:6:"750.00";s:8:"up_money";d:25;s:8:"up_pmana";i:0;s:8:"up_omana";i:0;s:8:"up_wmana";i:0;s:8:"up_nmana";i:0;s:8:"up_emana";i:0;s:6:"up_pop";i:0;}}s:8:"recruits";i:200;s:4:"ench";a:1:{s:12:"spellsLoaded";b:1;}}');


$village= new GetVillage($turn);

//print_r ($village->village);
?>